<template>
  <div>
    <div class="top-box">
      <div class="bg-box"><img src="../../../../assets/img/logo-my.png" alt="" width="100%"></div>
      <div class="content-box">
        <router-link to="/myEditor">
          <div class="user">
            <img :src="user.head_sculpture ? user.head_sculpture : avatar" alt="" width="78" height="78">
            <p>{{ user.name }}</p>
          </div>
        </router-link>
      </div>
      <div class="message">
        <router-link to="/message">
          <div class="icon-box"><img src="../../../../assets/img/icon-message.png" alt="" width="100%"></div>
          <div class="dot" v-if="showDot"></div>
        </router-link>
      </div>
    </div>
    <div class="card-list">
      <div class="card">
        <router-link to="/check">
          <div class="card-title check">
            <span>练琴打卡</span>
            <img src="../../../../assets/img/icon-punch.png" alt="" width="20">
          </div>
        </router-link>
      </div>
      <div class="card course">
        <router-link to="/index">
          <p class="card-title check">
            <span>我的课程</span>
            <img src="../../../../assets/img/icon-arrow.png" alt="" width="18">
          </p>
        </router-link>
        <van-cell-group>
          <van-cell v-for="(item, index) in courseList" :key="index" v-if="!item.show">
            <div class="check-item">
              <span style="font-weight: 600;">{{ item.kcname }}</span>
              <span style="font-size: 12px;">{{ item.time }}</span>
              <span class="check-in" @click="courseCheck(item.profile_id)">签到</span>
            </div>
          </van-cell>
        </van-cell-group>
      </div>
      <div class="card teacher">
        <p class="card-title">我的老师</p>
        <van-cell-group>
          <van-cell v-for="(item, index) in teacherList" :key="index" :title="`${item.name}老师`" v-if="!item.show">
            <div class="teacher-item">
              <span>{{ item.kcname }}</span>
              <router-link :to="`/poster?tid=${item.id}`">
                <img src="../../../../assets/img/icon-forward.png" alt="" width="18">
              </router-link>
            </div>
          </van-cell>
        </van-cell-group>
      </div>
      <honor class="card" :showAdd="true"/>
    </div>
  </div>
</template>

<script>
import honor from '@/components/honor'
import api from '@/api'
import {datetimeFormat} from '@/utils'
export default {
  name: 'index',
  data () {
    return {
      user: {},
      avatar: require('@/assets/img/icon-avatar.png'),
      courseList: [],
      teacherList: [],
      showDot: false
    }
  },
  methods: {
    getUserInfo () {
      let token = localStorage.getItem('token')
      api.getUserInfo({
        params: {token},
        callback: res => {
          if (res.data.error_code === 0) {
            window.sessionStorage.setItem('user', JSON.stringify(res.data.data))
            let wxUser = {}
            if (localStorage.getItem('userInfo')) {
              wxUser = JSON.parse(localStorage.getItem('userInfo'))
            }
            this.user = res.data.data
            if (!this.user.head_sculpture && wxUser.headimgurl) {
              this.user.head_sculpture = wxUser.headimgurl
            }
            this.getTeacher()
            this.getCourse()
            this.getDot()
            this.$store.commit('SET_STUDENT', res.data.data)
          } else {
            this.$toast({
              message: '登录过期，请重新登录！',
              forbidClick: false,
              duration: 1000,
              onClose: () => {
                localStorage.removeItem('userInfo')
                localStorage.removeItem('token')
                this.$router.push('/')
              }
            })
          }
        }
      })
    },
    getTeacher () {
      api.studentTeacherMy({
        params: {student_id: this.user.id},
        callback: res => {
          if (res.data.error_code === 0) {
            this.teacherList = res.data.data
            let temp = []
            this.teacherList.forEach(item => {
              if (item.qian === 1 || (item.type === 3 && item.cstatus === 1)) {
                item.show = true
              }
              if (temp.indexOf(item.name) === -1) temp.push(item.name)
            })
            sessionStorage.setItem('myTeacher', temp.join(','))
          }
        }
      })
    },
    getCourse () {
      let user = JSON.parse(window.sessionStorage.getItem('user'))
      api.studentCourse({
        params: {student_id: user.id},
        callback: res => {
          if (res.data.error_code === 0) {
            this.courseList = res.data.data
            let temp = []
            this.courseList.forEach(item => {
              if (item.sktime && item.xktime) {
                let sk = datetimeFormat(new Date(Number(item.sktime) * 1000))
                let xk = datetimeFormat(new Date(Number(item.xktime) * 1000))
                item.time = sk + '~' + xk.split(' ')[1]
              }
              temp.push(item.kcname)
              if (item.type === 3 && (item.dstatus === 1 || item.cstatus === 1)) {
                item.show = true
              }
            })
            sessionStorage.setItem('myCourse', temp.join(','))
          }
        }
      })
    },
    courseCheck (id) {
      let params = {
        profile_id: id,
        student_id: this.user.id
      }
      api.studentCheckMy({
        params,
        callback: res => {
          this.$toast({
            message: res.data.message,
            duration: 1000,
            onClose: () => {
              this.getTeacher()
              this.getCourse()
            }
          })
        }
      })
    },
    getDot () {
      api.getMessageIndex({
        params: {id: JSON.parse(sessionStorage.getItem('user')).id},
        callback: res => {
          if (res.data.error_code === 0) {
            Object.keys(res.data.data).forEach(key => {
              if (key.indexOf('new') !== -1 && res.data.data[key] === 2) {
                this.showDot = true
              }
            })
          }
        }
      })
    }
  },
  components: {
    honor
  },
  created () {
    this.getUserInfo()
  }
}
</script>

<style scoped>
  .top-box{
    position: relative;
  }
  .top-box img{
    display: block;
  }
  .top-box .content-box{
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 16px;
    box-sizing: border-box;
  }
  .content-box .user{
    text-align: center;
    font-family: PingFangBlod;
    color: #1E1E1E;
    font-size: 18px;
  }
  .user img{
    border-radius: 50%;
    overflow: hidden;
  }
  .message{
    position: absolute;
    top: 16px;
    right: 10px;
    width: 30px;
    height: 30px;
  }
  .message .icon-box{
    padding: 3px;
    box-sizing: border-box;
    background-color: rgba(0,0,0,.3);
    border-radius: 50%;
    overflow: hidden;
  }
  .message .dot{
    width: 8px;
    height: 8px;
    position: absolute;
    background-color: #ee0a24;
    overflow: hidden;
    border-radius: 50%;
    top: 0;
    right: 0;
  }
  .card-list{
    background-color: #F2F2F2;
    padding: 15px;
  }
  .card{
    background:rgba(255,255,255,1);
    border-radius:6px;
    margin-bottom: 10px;
    overflow: hidden;
  }
  .card-title{
    font-size:18px;
    font-family:PingFang SC;
    font-weight:bold;
    line-height:25px;
    color:rgba(30,30,30,1);
    padding: 16px;
  }
  .card-title.check{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .check-in{
    color: #3A7FE2;
  }
  .teacher-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .check-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>
